 package com.gmrz.uaf.common;

 import com.gmrz.uaf.crypto.UAFCryptoModule;
 import com.gmrz.uaf.db.UAFDBModule;
 import com.gmrz.uaf.protocol.v1.processor.UAFProcessorModule;
 import com.google.inject.Guice;
 import com.google.inject.Injector;
 import com.google.inject.Key;
 import org.apache.logging.log4j.LogManager;
 import org.apache.logging.log4j.Logger;

 import java.util.ArrayList;
 import java.util.List;
 import java.util.Map;
 import java.util.Properties;


 public class GuiceUtil
 {
   private static final Logger LOG = LogManager.getLogger(GuiceUtil.class);

   private static Injector injector = null;
   private static Properties uafProperties = new Properties();


   public static synchronized void setUafProperties(Properties uasconfig){

       uafProperties=uasconfig;
   }
 
   public static Injector getProcessorInjector() {
     if (injector == null) {
       createProcessorInjector();
     }
     return injector;
   }
 
   private static synchronized void createProcessorInjector()
   {
     if (injector == null) {
       // 加密
       UAFCryptoModule uafCryptoModule = new UAFCryptoModule(uafProperties);
       // 数据库dao
       UAFDBModule uafDBModule = new UAFDBModule(uafProperties);
       // procssor处理
       UAFProcessorModule uafProcessorModule = new UAFProcessorModule(uafProperties);
 
       injector = Guice.createInjector(uafCryptoModule, uafDBModule, uafProcessorModule);
 
       LOG.debug("Logging binding configurations - ");
       logBindings(injector);
     }
   }
 
   private static void logBindings(Injector injector) {
     Map map = injector.getBindings();
     List<Key> keys = new ArrayList(map.keySet());
     for (Key key : keys)
       LOG.debug(key.toString() + ": " + map.get(key));
   }
 }
